package org.jeecg.modules.lock.controller;

import io.swagger.annotations.Api;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.util.UUIDGenerator;
import org.jeecg.modules.lock.entity.LwgyDoorLock;
import org.jeecg.modules.lock.service.LwgyDoorLockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.Map;

/**
 * Created by IntelliJ IDEA
 *
 * @Description:
 * @Author : M-JGG
 * @create 2024/4/2 15:04
 */
@Api(tags="门锁-数据上报")
@RestController
@RequestMapping("/belongDoorLock")
@Slf4j
public class LwgyDoorLockDataController{

    @Autowired
    private LwgyDoorLockService lwgyDoorLockService;

    /**
     * 门锁-数据上报
     */
    @AutoLog(value = "门锁-数据上报")
    @ApiOperation(value="门锁-数据上报",notes = "门锁-数据上报")
    @PostMapping(value = "/receiveDoorLockData")
    public Result<?> receiveDoorLockData(@RequestBody Map<String, Object> msg ) {
        try {
            if (msg == null || msg.isEmpty()) {
                return Result.error(1,"推送数据为空");
            }
            log.info("一条门锁推送数据----------:" + msg);
            // 提取参数
            JSONObject json = JSONObject.fromObject(msg);
            log.info("json的内容是: ----------" + json);
            JSONObject data = json.getJSONObject("data");//Json串
            log.info("data的参数都有: ----------" + data);
            String requestId = json.getString("requestId");//关联消息的Id
            String method = json.getString("method");//业务方法
            Long uploadTime = json.getLong("uploadTime");//上报时间
            String sign = json.getString("sign");//签名

            // 提取data中的参数
            String roomId = data.getString("roomId");//房间ID
            String logId = data.getString("logId");//日志ID
            int logType = data.getInt("logType");//日志类型
            int lockType = data.getInt("lockType");//门锁类型
            String lockMac = data.getString("lockMac");//门锁mac
            long updateTime = data.getLong("updateTime");//日志触发时间
            String logLevel = data.getString("logLevel");//日志等级
            long createTime = data.getLong("createTime");//日志创建时间
            String logAlert = data.getString("logAlert");//日志描述

            LocalDateTime uploadDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(uploadTime * 1000), ZoneId.systemDefault());
            LocalDateTime createDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(createTime * 1000), ZoneId.systemDefault());
            LocalDateTime updateDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(updateTime * 1000), ZoneId.systemDefault());
            Date uploadDate = Date.from(uploadDateTime.atZone(ZoneId.systemDefault()).toInstant());
            Date createDate = Date.from(createDateTime.atZone(ZoneId.systemDefault()).toInstant());
            Date updateDate = Date.from(updateDateTime.atZone(ZoneId.systemDefault()).toInstant());

            LwgyDoorLock lwgyDoorLock = new LwgyDoorLock();
            String id = UUIDGenerator.generate();
            lwgyDoorLock.setId(id);
            lwgyDoorLock.setRoomId(roomId);
            lwgyDoorLock.setRequestId(requestId);
            lwgyDoorLock.setUploadTime(uploadDate);
            lwgyDoorLock.setLogId(logId);
            lwgyDoorLock.setLogType(String.valueOf(logType));
            lwgyDoorLock.setLockType(String.valueOf(lockType));
            lwgyDoorLock.setLockMac(lockMac);
            lwgyDoorLock.setCreateTime(createDate);
            lwgyDoorLock.setLogLevel(logLevel);
            lwgyDoorLock.setUpdateTime(updateDate);
            lwgyDoorLock.setLogAlert(logAlert);
            lwgyDoorLock.setSign(sign);

            lwgyDoorLockService.save(lwgyDoorLock);
            //lwgyDoorLockService.handleDoorLock(lwgyDoorLock);
        } catch (JSONException e){
            return Result.error(1,"数据格式不正确");
        }catch (Exception e){
            log.error("消息处理异常：", e);
            return Result.error(1,"消息处理异常:" + e.getMessage());
        }
        return Result.OK("操作成功");

    }
}
